package com.hzh.cusviewdemo.Util;

import com.blankj.utilcode.util.TimeUtils;
import com.hzh.cusviewdemo.Bean.MyMusicData;
import com.hzh.cusviewdemo.Bean.MySongMenu;
import com.hzh.cusviewdemo.Bean.SearchRecord;
import com.hzh.cusviewdemo.Music.Constant;
import com.hzh.cusviewdemo.Music.MyMusicUtil;
import com.orm.SugarRecord;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;

/**
 * Sugar数据操作工具类
 */
public class SugarUtils {

    /*************************保存播放列表*******************************/

    /**
     * 添加歌曲数据
     */
    public static void addMusicData(MyMusicData data) {
        if (findMusicDataByRidOrSid(data.getUrl()) != null) {
            return;
        }
        SugarRecord.save(data);
    }

    /**
     * @return
     */
    public static List<MyMusicData> findMusicData() {
        List<MyMusicData> result = new ArrayList<>();
        Iterator<MyMusicData> musicData = SugarRecord.findAll(MyMusicData.class);
        while (musicData.hasNext()) {
            result.add(musicData.next());
        }
        return result;
    }

    public static List<MyMusicData> findMusicDataDesc() {
        List<MyMusicData> result = new ArrayList<>();
        Iterator<MyMusicData> musicData = SugarRecord.findAsIterator(MyMusicData.class, "", null, "", "create_time DESC", "");
        while (musicData.hasNext()) {
            result.add(musicData.next());
        }
        // Log.e(Config.TAG, "遍历result: " + result.size());
        return result;
    }

    /**
     * 通过根据歌曲Url查询歌曲
     *
     * @return
     */
    public static MyMusicData findMusicDataByUrl(String url) {
        MyMusicData result = null;
        for (MyMusicData musicData : findMusicData()) {
            if (musicData.getUrl().equals(url)) {
                result = musicData;
                break;
            }
        }
        return result;
    }

    /**
     * 通过根据歌曲rid查询歌曲
     *
     * @return
     */
    public static MyMusicData findMusicDataByRidOrSid(String rid) {
        MyMusicData result = null;
        for (MyMusicData musicData : findMusicData()) {
            if (musicData.getRid().equalsIgnoreCase(rid) || musicData.getSid().equalsIgnoreCase(rid)) {
                result = musicData;
                break;
            }
        }
        return result;
    }

    /**
     * 倒序获取歌单下的歌曲
     *
     * @param menu
     * @return
     */
    public static List<MyMusicData> findMusicDataByMenu(String menu) {
        List<MyMusicData> list = new ArrayList<>();
        for (MyMusicData musicData : findMusicDataDesc()) {
            // Log.e(Config.TAG, "musicdata:" + musicData.getMenu());
            if (musicData.getMenu().equals(menu)) {
                list.add(musicData);
            }
        }
        return list;
    }


    /**
     * 删除歌曲
     */
    public static void deleteMusicData(String rid) {
        MyMusicData musicData = findMusicDataByRidOrSid(rid);
        if (musicData != null) {
            musicData.delete();
        }
    }

    /*********************************************************/

    /***
     * 获取所有的搜索历史记录
     * @return
     */
    public static List<SearchRecord> getAllSearchRecord() {
        List<SearchRecord> result = new ArrayList<>();
        Iterator<SearchRecord> dataList = SugarRecord.findAsIterator(SearchRecord.class, "", null, "", "create_time DESC", "");
        while (dataList.hasNext()) {
            result.add(dataList.next());
        }
        return result;
    }

    /**
     * 查询单条记录
     *
     * @param content
     * @return
     */
    public static SearchRecord getRecordByContent(String content) {
        if (getAllSearchRecord().size() == 0) {
            return null;
        }
        for (SearchRecord searchRecord : getAllSearchRecord()) {
            if (searchRecord.getContent().equals(content)) {
                return searchRecord;
            }
        }
        return null;
    }

    /*********************************歌单***************************************/
    /***
     * 获取所有的歌单
     * @return
     */
    public static List<MySongMenu> getAllMySongMenu() {
        List<MySongMenu> result = new ArrayList<>();
        Iterator<MySongMenu> dataList = SugarRecord.findAsIterator(MySongMenu.class, "", null, "", "create_time DESC", "");
        while (dataList.hasNext()) {
            result.add(dataList.next());
        }
        if (result.isEmpty() && MyMusicUtil.getStringShared(Constant.IS_FIRST_USE_APP).equals("0")) {
            // 第一次使用本APP，创建我喜欢和全部歌曲歌单
            MySongMenu myAllSongMenu = new MySongMenu();
            myAllSongMenu.setName("全部歌曲");
            myAllSongMenu.setPic("");
            myAllSongMenu.setNum(0);
            myAllSongMenu.setCreateTime(TimeUtils.getNowString());
            myAllSongMenu.setUpdateTime(TimeUtils.getNowString());
            myAllSongMenu.save();

            MySongMenu myFavoriteMenu = new MySongMenu();
            myFavoriteMenu.setName("我喜欢的音乐");
            myFavoriteMenu.setPic("");
            myFavoriteMenu.setNum(0);
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.SECOND, 2);
            myFavoriteMenu.setCreateTime(TimeUtils.date2String(calendar.getTime()));
            myFavoriteMenu.setUpdateTime(TimeUtils.getNowString());
            myFavoriteMenu.save();

            result.add(myFavoriteMenu);
            result.add(myAllSongMenu);

            MyMusicUtil.setShared(Constant.IS_FIRST_USE_APP, "1");
        }
        return result;
    }

    public static MySongMenu getSongMenuByName(String menuName) {
        for (MySongMenu allMySongMenu : getAllMySongMenu()) {
            if (allMySongMenu.getName().equals(menuName)) {
                return allMySongMenu;
            }
        }
        return null;
    }
}
